﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_PersonJobSrhForm
 * 类型：       控制层
 * 功能说明：   职位信息列表页面
 * 作者：       黄鑫
 * 创建日期：   2007/10/30
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;

public partial class App_Input_PersonJobSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_PersonJobSrhForm));

    private const string SELECT_ALL = "-1";

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!Page.IsPostBack)
        {
            // 取得当前登录的用户id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();

            initPageControls();

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["GROUP_ID"] = Convert.ToString(Context.Items["CMB_GROUP_ID"]);
                lstGroup.SelectedValue = Convert.ToString(this.ViewState["GROUP_ID"]);
                if (lstGroup.SelectedValue != SELECT_ALL)
                {
                    setLstJob(Convert.ToString(this.ViewState["GROUP_ID"]),"");
                }
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["JOB_ID"] = Convert.ToString(Context.Items["CMB_JOB_ID"]);
                lstJob.SelectedValue = Convert.ToString(this.ViewState["JOB_ID"]);
            }
        }
    }

    // bind common catalog data 
    private void initPageControls()
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        IList lstJob1 = dao.ListJob(bean);
        lstJob1.Insert(0,new PersonJobBean());
        IList lstGroup1 = dao.ListGroup(bean);
        lstGroup1.Insert(0, new PersonJobBean());

        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();

        // 部门列表
        lstGroup.DataSource = lstGroup1;
        lstGroup.DataBind();

    }


    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // get current doctor responsibility members list
        PersonJobDao dao = new PersonJobDao();
        IList lstRst = null;
        PersonJobBean bean = new PersonJobBean();
        if (! string.IsNullOrEmpty(Convert.ToString(this.ViewState["GROUP_ID"])))
        {
            bean.DepartId = Convert.ToString(this.ViewState["GROUP_ID"]);
        }
        if (! string.IsNullOrEmpty(Convert.ToString(this.ViewState["JOB_ID"])))
        {
            bean.JobId = Convert.ToInt32(this.ViewState["JOB_ID"]);
        }
        lstRst = dao.ListResult(bean);
        if (lstRst.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0034").ToString();
        }
        else
        {
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }
        setLstJob(bean.DepartId,Convert.ToString(bean.JobId));
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }


    protected void viewSrhRst_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        string jobId = null;
        int index = Convert.ToInt32(e.NewEditIndex);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        jobId = Convert.ToString(key.Value);
        string url = "~/App/Input/PersonJobForm.aspx";


        // 传递分页编号
        Context.Items["JOB_ID"] = jobId;
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);    
    }

    protected void viewSrhRst_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string jobId = null;
        int index = Convert.ToInt32(e.RowIndex);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        jobId = Convert.ToString(key.Value);


        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        bean.JobId = Convert.ToInt32(jobId);
        bean = dao.Find(bean);
        if (bean != null)
        {
            PersonJobBean beanDelete = new PersonJobBean();
            beanDelete.JobLevel = bean.JobLevel;
            IList lstResult = dao.CheckUsed(beanDelete);
            if (lstResult.Count != 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0033").ToString();
            }
            else
            {
                dao.Delete(bean);
                ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
            }
        }
    }
    

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
    }
    protected void btnSeach_Click(object sender, EventArgs e)
    {
        this.ViewState["GROUP_ID"] = lstGroup.SelectedValue;
        this.ViewState["JOB_ID"] = lstJob.SelectedValue;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/PersonJobForm.aspx";

        // 传递分页编号
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["CMB_STATUS_ID"] = Convert.ToString(this.ViewState["STATUS_ID"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);    
    }

    protected void setLstJob(string groupId,string jobId)
    {
        PersonJobDao dao = new PersonJobDao();
        PersonJobBean bean = new PersonJobBean();
        bean.GroupId = groupId;
        IList lstJob1 = dao.ListJob(bean);
        lstJob1.Insert(0, new PersonJobBean());


        // 职位列表
        lstJob.DataSource = lstJob1;
        lstJob.DataBind();
        if (!string.IsNullOrEmpty(Convert.ToString(jobId)))
        {
            lstJob.SelectedValue = Convert.ToString(jobId);
        }
    }
}
